package org.correct.util;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.correct.model.PageInfoItem;

public class PageInfoAccessor {
	private String fileName;
	private LinkedList<PageInfoItem> pageList = null;
	private PageInfoItem[] pageArray = null;
	private HashMap<Integer, PageInfoItem> pageMap = null;
	private int maxId;
	
	public static String separater = "<";
	
	public PageInfoAccessor(String fileName) {
		this.fileName = fileName;
	}
	
	public void load() {
		pageList = new LinkedList<PageInfoItem>();
		pageMap = new HashMap<Integer, PageInfoItem>();
		maxId = 0;
		
		try {
			BufferedReader r = new BufferedReader(new FileReader(fileName));
			String line = null;
			String[] args = null;
			PageInfoItem p = null;
			
			while((line = r.readLine()) != null) {
				args = line.split(PageInfoAccessor.separater);
				if(args != null && args.length > 0) {
					p = new PageInfoItem();
					
					p.id = Integer.parseInt(args[0]);
					p.url = args[1];
					p.encoding = args[2];
					p.fetchTime = Calendar.getInstance();
					p.title = "";
					p.pageRank = 0;
					
					if(p.id > maxId)
						maxId = p.id;
					
					if(args.length > 3) {
						p.fetchTime = Calendar.getInstance();
						p.fetchTime.setTimeInMillis(Long.parseLong(args[3]));
					}
					if(args.length > 4)
						p.title = args[4];
					if(args.length > 5)
						p.pageRank = Double.parseDouble(args[5]);
					if(args.length > 6){
						p.termCount = Integer.parseInt(args[6]);
					}
					
					pageList.add(p);
					pageMap.put(p.id, p);
				}
			}
			r.close();
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		pageArray = new PageInfoItem[maxId + 1];
		for(PageInfoItem page : pageList) {
			pageArray[page.id] = page;
		}
	}
	
	public void updateInfo(PageInfoItem.InfoType type, HashMap<Integer, Object> map) {
		PageInfoItem p = null;
		for(Map.Entry<Integer, Object> entry : map.entrySet()) {
			p = pageMap.get(entry.getKey());
			switch(type) {
				case title:
					p.title = (String)entry.getValue();
					break;
				case pageRank:
					p.pageRank = (Double)entry.getValue();
					break;
				case termCount:
					p.termCount = (Integer)entry.getValue();
					break;
			}
			pageMap.put(p.id, p);
		}
	}
	
	public List<PageInfoItem> getPageInfoItems() {
		if(pageList == null)
			load();
		return pageList;
	}
	
	public PageInfoItem getItem(int id) {
		return pageMap.get(id);
		
		/*
		if(id > maxId)
			return null;
		PageInfoItem item = pageArray[id];
		if(item != null)
			return item.clone();
		else
			return null;
		*/
	}
	
	public void savePageInfo() {
		try {
			PrintWriter w = new PrintWriter(fileName);
			
			for(PageInfoItem page : pageMap.values()) {
				w.println(
						page.id + PageInfoAccessor.separater +
						page.url + PageInfoAccessor.separater +
						page.encoding + PageInfoAccessor.separater +
						page.fetchTime.getTimeInMillis() + PageInfoAccessor.separater +
						page.title + PageInfoAccessor.separater + 
						page.pageRank + PageInfoAccessor.separater +
						page.termCount
						);
			}
			
			w.flush();
			w.close();
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
}
